﻿using CommonDevelop.Scene;
using CommonEngine;
using CommonFP;
using System;
using System.Threading.Tasks;

namespace CommonDevelop.SSO
{
    /// <summary>
    /// 适配器
    /// </summary>
    public class SSOAdapter
    {
        private SSORegistry _Adapter { get; set; }
        private Func<string, UserInfo> GetUserInfo { get; set; }
        /// <summary>
        /// 构造
        /// </summary>
        public SSOAdapter(Func<string, UserInfo> GetData, Func<UserSSO, Task<ValueTuple>> Log = null)
        {
            this.GetUserInfo = GetData;
            var saveLog = Log != null ? Log : async usso => await Task.FromResult(FP.Unit());
            this._Adapter = new SSORegistry(LoadInfo, saveLog);
        }
        private async Task<Option<UserSSO>> LoadInfo(string Uid)
        {
            if (Uid.IsEmpty())
                return await Task.FromResult(FP.None);

            var ui = this.GetUserInfo(Uid);
            if (ui == null)
                return await Task.FromResult(FP.None);

            UserSSO usso = new UserSSO(ui.UserId, string.Empty, ui.BusinessID);
            return usso;
        }

        /// <summary>
        /// 验证用户缓存1
        /// </summary>
        public UserInfo CheckingUser(string Uid, string Token)
        {
            if (Uid.IsEmpty() || Token.IsEmpty())
                return null;

            var Command = new CheckingUserCommand() { Token = Token };
            var v = this._Adapter.Lookup(Uid);
            v.Wait();
            var Process = v.Result.Outcome();
            if (Process == null)
                return null;

            var t = Process.Handle(Command);
            t.Wait();
            var userSSO = t.Result.Outcome(a => a);
            if (!userSSO.Errors.IsEmpty())
                return null;

            if (userSSO.result.HasValid)
            {
                if (userSSO.result.Token.Equal(Token) && this.GetUserInfo != null)
                {
                    var UInfo = this.GetUserInfo(userSSO.result.UserID);
                    if (UInfo != null)
                    {
                        UInfo.Token = userSSO.result.Token;
                        return UInfo;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 验证用户缓存2
        /// </summary>
        public bool Checking(string Uid, string Token)
        {
            if (Uid.IsEmpty() || Token.IsEmpty())
                return false;

            var Command = new CheckingUserCommand() { Token = Token };
            var v = this._Adapter.Lookup(Uid);
            v.Wait();
            var Process = v.Result.Outcome();
            if (Process == null)
                return false;

            var t = Process.Handle(Command);
            t.Wait();
            var userSSO = t.Result.Outcome(a => a);
            if (!userSSO.Errors.IsEmpty())
                return false;

            if (userSSO.result.HasValid)
                if (userSSO.result.Token.Equal(Token))
                    return true;

            return false;
        }

        /// <summary>
        /// 验证用户缓存3
        /// </summary>
        public UserInfo CheckUser(string Uid)
        {
            if (Uid.IsEmpty())
                return null;

            var v = this._Adapter.Lookup(Uid);
            v.Wait();
            var Process = v.Result.Outcome();
            if (Process == null)
                return null;

            var Command = new CheckUserCommand();
            var t = Process.Handle(Command);
            t.Wait();
            var userSSO = t.Result.Outcome(a => a);
            if (!userSSO.Errors.IsEmpty())
                return null;

            if (userSSO.result.HasValid)
            {
                if (this.GetUserInfo != null)
                {
                    var UInfo = this.GetUserInfo(userSSO.result.UserID);
                    if (UInfo != null)
                    {
                        UInfo.Token = userSSO.result.Token;
                        return UInfo;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 激活用户缓存
        /// </summary>
        public UserInfo ActivateUser(string Uid)
        {
            if (Uid.IsEmpty())
                return null;

            var Command = new ActivateUserCommand();
            var v = this._Adapter.Lookup(Uid);
            v.Wait();
            var Process = v.Result.Outcome();
            if (Process == null)
                return null;

            var t = Process.Handle(Command);
            t.Wait();
            var userSSO = t.Result.Outcome(a => a);
            if (!userSSO.Errors.IsEmpty())
                return null;

            if (this.GetUserInfo == null)
                return null;

            var UInfo = this.GetUserInfo(userSSO.result.UserID);
            if (UInfo != null)
            {
                UInfo.Token = userSSO.result.Token;
                return UInfo;
            }

            return null;
        }

        /// <summary>
        /// 注销用户缓存
        /// </summary>
        public bool LogoutUser(string Uid)
        {
            if (Uid.IsEmpty())
                return false;

            var Command = new LogoutUserCommand();
            var v = this._Adapter.Lookup(Uid);
            v.Wait();
            var Process = v.Result.Outcome();
            if (Process == null)
                return false;

            var t = Process.Handle(Command);
            t.Wait();
            var userSSO = t.Result.Outcome(a => a);
            if (!userSSO.Errors.IsEmpty())
                return false;
            else
                return true;
        }
    }
}
